package moodish.client;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;


import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


/**
 * 
 * This class represents the User panel that is added to the ClientGUI.
 * 
 * @author es1-2013-group2
 *
 */
public class UserPanel extends JPanel{


	private User user;
	private Talk talk;
	private ImageIcon imageMood;
	private JButton moodButton;
	private JLabel userLabel;
	private ClientGUI clientGUI;
	
	/**
	 * 
	 * The constructor creates a new User Panel with the parameter ClientGUI, User and Talk.
	 * Sets the layout of this panel.
	 * The actionListener create a new mood chooser (Allow the user to change his mood).
	 * 
	 * @param clientGUI
	 * @param user
	 * @param talk
	 */
	public UserPanel(final ClientGUI clientGUI, final User user, final Talk talk){

		this.clientGUI = clientGUI;
		this.user = user;
		this.talk = talk;
		imageMood = user.getImageIcon();
		moodButton = new JButton(imageMood);
		userLabel = new JLabel(user.getNickname());
		
		moodButton.setPreferredSize(new Dimension(20,20));
		moodButton.setBackground(Color.WHITE);
		moodButton.setEnabled(false);
		moodButton.addActionListener(new ActionListener() {
		

			@Override
			public void actionPerformed(ActionEvent arg0) {

				MoodChooser moodChooser = new MoodChooser(user, UserPanel.this);
			}
			
			
		});

		setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
		add(moodButton);
		add(Box.createRigidArea(new Dimension(15, 0)));
		add(userLabel);
		setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
	}
	
	/**
	 * 
	 * Update the mood of the user in the Talk (with date, user nickname and mood image icon).
	 * 
	 */
	public void updateUserState(){
		moodButton.setIcon(user.getImageIcon());
		Date date = new Date();
		//talk.addMoodMessage(date, user.getNickname(), user.getImageIcon());
		talk.addMoodMessage(date, user.getNickname(), user.getMood().name());
		clientGUI.sendMoodishMessage(user.getMood().name());
	}
	
	/**
	 * 
	 * Enable the mood button.
	 * 
	 */
	public void enableMoodButton(){
		moodButton.setEnabled(true);
	}
	
	/**
	 * 
	 * Disable the mood button.
	 * 
	 */
	public void disableMoodButton(){
		moodButton.setEnabled(false);
	}

}
